<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function(){
				$("#search").button({
		            icons: {
		                primary: "ui-icon-search"
		            }
				}).click(function(){
					if(validateEmpty()){
						$("#searchForm").submit();
					}
				});
				$("#btnExport").button({
		            icons: {
		                primary: "ui-icon-arrowthickstop-1-s"
		            }
				}).click(function(){
					if(validateEmpty()){
						window.location='${pageContext.request.contextPath}/acc7?app=ACC70401&job=export&provinceId='+$("#provinceId").val()+"&budgetYear="+$("#budgetYear").val()+"&month="+$("#month").val();
					}
				});
			});
		</script>
	</head>
	<body>
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="searchForm" id="searchForm" action="${pageContext.request.contextPath}/acc7" method="post">
		<input type="hidden" name="app" id="app" value="ACC70401"/>
		<input type="hidden" name="job" id="job" value="${job}"/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC70401}</b></th>
			</tr>
			<tr>
				<td width="20%" class="bold">จังหวัด : </td>
				<td>
					<c:if test="${userInfo.areaLevel =='3'}">
						<c:forEach var="data" items="${userInfo.listUserProvice}">
							${data.provinceName}
							<input type="hidden" name="provinceId" id="provinceId" value="${data.provinceId}">
						</c:forEach>
					</c:if>
					<c:if test="${userInfo.areaLevel=='1' || userInfo.areaLevel=='2'}">
						<select name="provinceId" id="provinceId">
							<option value="">เลือกทั้งหมด</option>
							<c:forEach var="data" items="${userInfo.listUserProvice}">
								<c:if test="${data.provinceId == provinceId}" var="provinceSeleted"/>
								<option value="${data.provinceId}" ${provinceSeleted?'selected':''}>${data.provinceName}</option>
							</c:forEach>
						</select>
						<!-- <span id="$provinceId" class="red">*</span> -->
					</c:if>
				</td>
			</tr>
			<tr>
				<td class="bold">ปีที่ออกรายงาน : </td>
				<td>
					<select name="budgetYear" id="budgetYear">
						<option value="">----- เลือก -----</option>
						<c:forEach items="${listBudgetYear}" var="data">
							<c:if test="${budgetYear == data.BUDGET_YEAR}" var="selected"/>
							<option value="${data.BUDGET_YEAR}" ${selected?'selected':''}>${data.BUDGET_YEAR}</option>
						</c:forEach>
					</select>
					<span id="$budgetYear" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td class="bold">เดือน : </td>
				<td>
					<select name="month" id="month">
						<option value="">----- เลือก -----</option>
							<option value="01" ${'01' == month?'selected':''}>มกราคม</option>
							<option value="02" ${'02' == month?'selected':''}>กุมภาพันธ์</option>
							<option value="03" ${'03' == month?'selected':''}>มีนาคม</option>
							<option value="04" ${'04' == month?'selected':''}>เมษายน</option>
							<option value="05" ${'05' == month?'selected':''}>พฤษภาคม</option>
							<option value="06" ${'06' == month?'selected':''}>มิถุนายน</option>
							<option value="07" ${'07' == month?'selected':''}>กรกฎาคม</option>
							<option value="08" ${'08' == month?'selected':''}>สิงหาคม</option>
							<option value="09" ${'09' == month?'selected':''}>กันยายน</option>
							<option value="10" ${'10' == month?'selected':''}>ตุลาคม</option>
							<option value="11" ${'11' == month?'selected':''}>พฤศจิกายน</option>
							<option value="12" ${'12' == month?'selected':''}>ธันวาคม</option>
					</select>
					<span id="$month" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<button type="button" name="search" id="search">ค้นหาข้อมูล</button>
					<button type="button" name="btnExport" id="btnExport">Download</button>
				</td>
			</tr>
		</table>
		<br/>
<%-- 		<c:if test="${!empty listData || !empty sumIncomeAmountList || !empty sumIncomeAmountList}"> --%>
<%-- 		<c:if test="${!empty sumIncomeAmountList || !empty sumIncomeAmountList}"> --%>
			<fieldset>
				<legend>ผลการค้นหาข้อมูล</legend>
					<table width="100%" class="acc67-grid ui-widget ui-widget-content" border="0">
						<tr>
							<th colspan="${sumCount+6}" align="center">สำนักงานสาธารณสุขจังหวัด ${provinceName}<br/>ทะเบียนคุมค่าบริหารจัดการ<br/>ประจำเดือน ${monthName} ${budgetYear}</th>
						</tr>
						<tr>
							<th width="8%" rowspan="2" class="ui-state-default">วัน เดือน ปี</th>
							<th width="8%" rowspan="2" class="ui-state-default">เลขที่เอกสารอ้างอิง</th>
							<th width="8%" rowspan="2" class="ui-state-default">ปีงบประมาณ</th>
							<th width="15%" rowspan="2" class="ui-state-default">รายการ</th>
							<th width="15%" rowspan="2" class="ui-state-default">รายรับ</th>
							<c:forEach items="${categoryLvl1}" var="data" >
								<c:if test="${data.COUNT_CATEGORY_PARENT > 0}">
									<th class="ui-state-default" colspan="${data.COUNT_CATEGORY_PARENT}">${data.CATEGORY_NAME}</th>
								</c:if>
								<c:if test="${data.COUNT_CATEGORY_PARENT == 0}">
									<th class="ui-state-default" width="5%" rowspan="2">${data.CATEGORY_NAME}</th>
								</c:if>
							</c:forEach>
							<th width="12%" rowspan="2" class="ui-state-default">จ่ายรวม</th>
							<th width="12%" rowspan="2" class="ui-state-default">คงเหลือ</th>
						</tr>
						<tr>
							<c:forEach items="${categoryLvl1}" var="categoryLvl1">
								<c:set var="daraList" value="${el:getExpenseCategory(categoryLvl1.CATEGORY_ID,'getPrarentByCatId')}"/>
								<c:forEach items="${daraList}" var="daraList">
									<th width="5%" class="ui-state-default">${daraList.categoryName}</th>
								</c:forEach>
							</c:forEach>
						</tr>
						<c:set var="remain" value="0"/>
						
						<c:if test="${result != null}">
							
							<tr class="grid-tr-${x.count % 2}">
								<td align="center">&nbsp;</td>
								<td align="center">&nbsp;</td>
								<td align="center">&nbsp;</td>
								<td>ยอดคงเหลือ</td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${result}" />&nbsp;</td>
								<td align="right" colspan="${sumCount}">&nbsp;</td>
								<td align="right">0.00</td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${result}" />&nbsp;</td>
								<c:set var="remain" value="${remain+result}"/>
							</tr>
						
						</c:if>
							
						<c:forEach items="${sumIncomeAmountSAPList}" var="daraSAPList" varStatus="x">
							<tr class="grid-tr-${x.count % 2}">
								<td align="center">${el:dateTh(daraSAPList.INCOME_DATE)}</td>
								<td align="center">${daraSAPList.REF_NO}</td>
								<td align="center">${daraSAPList.BUDGET_YEAR}</td>
								<td>&nbsp;รายการจาก SAP</td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${daraSAPList.SUM_INCOME}" />&nbsp;</td>
								<td align="right" colspan="${sumCount}">&nbsp;</td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="0" />&nbsp;</td>
								
									<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${remain + daraSAPList.SUM_INCOME}" />&nbsp;</td>
								
								<c:set var="remain" value="${remain + daraSAPList.SUM_INCOME}"/>
							</tr>
						</c:forEach>
						<c:forEach items="${sumIncomeAmountList}" var="daraIncomeList" varStatus="x">
							<tr class="grid-tr-${x.count % 2}">
								<td align="center">${el:dateTh(daraIncomeList.INCOME_DATE)}</td>
								<td align="center">${daraIncomeList.REF_NO}</td>
								<td align="center">${daraIncomeList.BUDGET_YEAR}</td>
								<td>${daraIncomeList.INCOME_DESC}</td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${daraIncomeList.SUM_INCOME}" /></td>
								<td align="right" colspan="${sumCount}">&nbsp;</td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="0" /></td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${remain + daraIncomeList.SUM_INCOME}" />&nbsp;</td>
								<c:set var="remain" value="${remain + daraIncomeList.SUM_INCOME}"/>
							</tr>
						</c:forEach>
						<c:forEach items="${listData}" var="data1" varStatus="x">
							<tr class="grid-tr-${x.count % 2}">
								<td align="center">${el:dateTh(data1.EXPENSE_DATE)}</td>
								<td align="center">${data1.REF_NO}</td>
								<td align="center">${data1.BUDGET_YEAR}</td>
								<td>${data1.EXPENSE_DESC}</td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="0"/></td>
								<c:set var="sumAmount" value="0"/>
								<c:forEach items="${categoryLvl1}" var="categoryLvl1">
									<c:set var="dataList" value="${el:getExpenseCategory(categoryLvl1.CATEGORY_ID,'getAll')}"/>
									<c:forEach items="${dataList}" var="dataList">
										<%-- <c:if test="${dataList.categoryId == data1.CATEGORY_ID}">
											<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${data1.EXPENSE_AMOUNT}" /></td>
										</c:if>
										<c:if test="${dataList.categoryId != data1.CATEGORY_ID}">
											<td align="right">&nbsp;</td>
										</c:if> --%>
										<c:set var="amount" value="${el:getAmountByDocNo(data1.DOC_NO,dataList.categoryId)}"/>
										<td align="right">&nbsp;
											<c:if test="${amount != '0'}">
												<fmt:formatNumber pattern="#,##0.00" value="${amount}"/>
											</c:if>
										</td>
										<c:set var="sumAmount" value="${amount+sumAmount}"/>
									</c:forEach>
								</c:forEach>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${sumAmount}" /></td>
								<td align="right"><fmt:formatNumber pattern="#,##0.00" value="${remain - sumAmount}" /></td>
								<c:set var="remain" value="${remain - sumAmount}"/>
							</tr>
						</c:forEach>
					</table>
			</fieldset>
<%-- 		</c:if> --%>
		</form>
	</body>
</html>